#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define repp(i, n) for(ll i = 1; i <= n; ++i)
#define forr(i, l, r) for(ll i = l; i <= r; ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define l i * 2 + tmp
#define r i * 2 + 1 - tmp
const ll N = 200005;
ll n, m;
ll a0[N], c[N], X[4], a[N];
bool d[20][N];
vector<pair<ll, bool> > v, w;
void SOLVE() {
    rep(i, 4) cin >> X[i];
    ll ANS = 0;
    rep(id, m) {
        rep(i, n) a[i] = a0[i] ^ X[(i + 1) % 4];
        ll K = 0;
        while((1 << K) < c[id]) K++;
        forr(i, c[id], (1 << K) - 1) a[i] = -1;
        ll ans = 0;
        rep(s, (1 << K)) {
            v.clear();
            rep(i, (1 << K)) v.pb(mp(a[i], i == s));
            rep(R, K) {
                w.clear();
                rep(i, v.size() / 2) {
                    bool tmp = d[R][i];
                    if(v[l].fi >= 0) {
                        if(v[l].fi >= R + 1) w.pb(v[l]);
                        else w.pb(v[r]);
                    }
                    else {
                        if(v[r].se) w.pb(v[r]);
                        else w.pb(v[l]);
                    }
                }
                swap(v, w);
            }
            ans += v[0].se * (s + 1);
        }
        ANS ^= ans * (id + 1);
    }
    cout << ANS << endl;
}
int main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ll T;
    cin >> n >> m;
    rep(i, n) cin >> a0[i];
    rep(i, m) cin >> c[i];
    ll K = 0;
    while((1 << K) < n) K++;
    string st;
    rep(i, K) {
        cin >> st;
        rep(j, (1 << (K - i - 1))) d[i][j] = st[j] - '0';
    }
    cin >> T;
    while(T--) SOLVE();
    return 0;
}